using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Koala.Pro.Common;
using Koala.Pro.Core.Models;
using Koala.Pro.Helpers;
using Koala.Pro.ViewModels;
using Wpf.Ui.Controls;

namespace Koala.Pro.Views.Pages
{
    public partial class BlackIPEditPage : System.Windows.Window, INavigableView<BlackIPEditViewModel>
    {
        public BlackIPEditViewModel ViewModel { get; }

        public BlackIPEditPage(BlackIPEditViewModel viewModel)
        {
            ViewModel = viewModel;
            DataContext = this;

            InitializeComponent();
        }

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);
            ControlHelper.SetDirtyWindow(this);
        }

        [RelayCommand]
        private void Back()
        {
            this.Close();
        }

        [RelayCommand]
        private async Task Save()
        {
            var item = ViewModel.Item;
            if (item == null)
                return;

            #region 对模型数据进行判断
            if (item.Name.IsNullOrWhiteSpace())
            {
                var message = "请输入Name";
                Growl.ErrorGlobal(message);
                txtName.Focus();
                return;
            }
             else if (item.IPStart.IsNullOrWhiteSpace())
            {
                var message = "请输入IPStart";
                Growl.ErrorGlobal(message);
                txtIPStart.Focus();
                return;
            }
             else if (item.IPEnd.IsNullOrWhiteSpace())
            {
                var message = "请输入IPEnd";
                Growl.ErrorGlobal(message);
                txtIPEnd.Focus();
                return;
            }

            #endregion

            await ViewModel.Save();
            if (ViewModel.Result.Success)
            {                
                this.Back();
            }
        }
    }
}
